package cn.firecode.data_collect.controller;
import	java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;


import cn.firecode.data_collect.domain.entity.CollectDataT;
import cn.firecode.data_collect.domain.entity.CollectUserT;
import cn.firecode.data_collect.service.CollectUserTService;
import cn.firecode.data_collect.utils.DataResponse;
import cn.firecode.data_collect.utils.JwtHelper;
import cn.firecode.data_collect.utils.RedisUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class CollectUserTController {


    @Autowired
    private CollectUserTService userTService;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private RedisUtil  redisUtil;

    @Value("${collect.Salt}")
    private String salt;


    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public DataResponse add(String username, String password, Integer currentuser) {
        QueryWrapper<CollectUserT> wrapper = new QueryWrapper();
        wrapper.eq("user_name",username);
        CollectUserT has = userTService.getOne(wrapper);
        if(has!=null){
            return DataResponse.failed(1,"新增用户失败，该用户名存在");
        }
        CollectUserT userT = new CollectUserT();
        userT.setUserName(username);
        userT.setUserPassword(SecureUtil.md5(password+salt));
        boolean isSucceed = userTService.saveOrUpdate(userT);
        if(isSucceed){
            return DataResponse.succeed("添加成功",username);
        } else {
            return DataResponse.failed(1,"添加失败");
        }
    }

    @RequestMapping(value = "/login" ,method = RequestMethod.POST)
    public DataResponse login(String username, String password){
        String password_md5 = SecureUtil.md5(password+salt);
        log.info("password_md5:"+password_md5);
        QueryWrapper<CollectUserT> wrapper = new QueryWrapper();
        wrapper.eq("user_name",username);
        CollectUserT one = userTService.getOne(wrapper);
        if(one != null){
            if(!password_md5.equalsIgnoreCase(one.getUserPassword())){
                return DataResponse.failed(1,"密码错误");
            }
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put("userid", one.getUserId());
            JSONObject jsonObject = jwtHelper.generateToken(claims);
            redisUtil.set("currentUser"+one.getUserId(),one,60*60*24);
            return DataResponse.succeed("登录成功",jsonObject.getString("token"));
        }else{
            return DataResponse.failed(1,"该用户不存在");
        }
    }

    @RequestMapping(value = "/delete/{userid}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("userid") Integer userid){
        CollectUserT byId = userTService.getById(userid);
        if(byId==null){
            return DataResponse.failed(1,"该用户不存在");
        }
        boolean result = userTService.removeById(userid);
        if (result){
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }
    }



}
